package org.stvd.repository.admin.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.UserRole;
import org.stvd.repository.admin.UserRoleDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("UserRoleDao")
public class UserRoleDaoImpl extends BaseDaoImpl<UserRole> implements UserRoleDao {

    @Override
    public List<UserRole> findUserRoleByUid(String userId, String guid) {
        return findByHQL("from UserRole T where T.userid= ?0 and T.guid = ?1", userId, guid);
    }
    
    @Override
    public UserRole findUserRole(String userId, String roleId, String guid) {
        List<UserRole> userRoles = findByHQL("from UserRole T where T.userId= ?0 and T.roleid = ?1 and T.guid = ?2", userId, roleId, guid);
        if(userRoles !=null && userRoles.size()>0) {
            return userRoles.get(0);
        }
        return null;
    }

    @Override
    @Transactional
    public void insert(String userId, String[] roleIds, String guid) {
        em.createQuery("delete from UserRole where userId = :userId and guid = :guid")
        	.setParameter("userId", userId)
        	.setParameter("guid", guid)
            .executeUpdate();
        em.flush();
        if (roleIds != null) {
            for (String roleId : roleIds) {
                UserRole userroleTmp = new UserRole();
                userroleTmp.setId(StringUtil.getRandomUUID());
                userroleTmp.setRoleId(roleId);
                userroleTmp.setUserId(userId);
                userroleTmp.setGuid(guid);
                insert(userroleTmp);
            }
        }
    }
    
    @Override
    @Transactional
    public void insertUserRole(String userId, String roleId, String guid) {
        if (findUserRole(userId, roleId, guid) == null) {
            UserRole userroleTmp = new UserRole();
            userroleTmp.setId(StringUtil.getRandomUUID());
            userroleTmp.setRoleId(roleId);
            userroleTmp.setUserId(userId);
            userroleTmp.setGuid(guid);
            insert(userroleTmp);
        }
    }
    
    @Override
    @Transactional
    public void deleteUserRole(String userId, String roleId, String guid) {
        if (findUserRole(userId, roleId, guid) != null) {
            em.createQuery("delete from UserRole T where T.userId= :userId and T.roleId = :roleId and T.guid = :guid")
            .setParameter("userid", userId)
            .setParameter("roleid", roleId)
            .setParameter("roleid", guid)
            .executeUpdate();
            em.flush();
        }
    }
    
    
    @Override
    public List<UserRole> findUserRoleByRoleid(String roleId, String guid) {
        return findByHQL("from UserRole T where T.roleId= ?0 and T.guid = ?1", roleId, guid);
    }
    
    /**
     * 更新用户userId旧角色oldRole为新角色newRole
     * @param userRole 要更改角色的UserRole实体
     * @param newRole 新角色
     */
    @Override
    @Transactional
    public void changeUsersRole(UserRole userRole, String newRole, String guid) {
        if (userRole != null) {
            //删除userRole记录
            em.createQuery("delete from UserRole T "
                    + "where T.userId=:userId and T.roleId=:roleId and T.guid = :guid ")
                    .setParameter("userId", userRole.getUserId())
                    .setParameter("roleId", userRole.getRoleId())
                    .setParameter("guid", guid)
                    .executeUpdate();
            em.flush();
        }
        //新增UserRole记录
        if(newRole!=null && !"".equals(newRole)) {
            UserRole userroleTmp = new UserRole();
            userroleTmp.setId(StringUtil.getRandomUUID());
            userroleTmp.setUserId(userRole.getUserId());
            userroleTmp.setRoleId(newRole);
            userroleTmp.setGuid(guid);
            insert(userroleTmp);
        }
    }

	@Override
	public UserRole getUserRoleByUesrRoleCode(String userId, String roleCode, String guid) {
		String hql = "SELECT T1 FROM UserRole T1, Roles T2 WHERE T1.roleId = T2.roleId "
				+ " AND T1.userId = ?0 AND T2.roleCode = ?1 AND T2.guid = ?2";
		List<UserRole> list = findByHQL(hql, userId, roleCode, guid);
		if(list!=null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
